package de.hs.rm;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.ScrollPane;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;


public class TesterGUI extends JFrame {

        public JLabel lbStatus;
        public JButton btRegister;
        public JButton btUnregister;
        public JTextArea taLog;
		public JTextField tfRegisterObjName;
		public JTextField tfRegisterClassName;
		public JTextField tfUnregister;
		
		public JList lRegister;
		public DefaultListModel lRegListModel;
		public JList lUnregister;
		public DefaultListModel lUnregListModel;
        
        private final static String newline = "\n";
        
        public TesterGUI()
        {
            super("JMXtoUPnpTester");
            setSize(700,250);
            setLocation(100,100);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            JPanel panel = new JPanel();
            panel.setBackground(Color.white);
            panel.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            this.add(panel);
            
//            tfRegisterObjName = new JTextField();
//            gbc.fill = GridBagConstraints.HORIZONTAL;
//            gbc.weightx = 2.0;
//            gbc.gridx = 0;
//            gbc.gridy = 0;
//            //gbc.gridwidth = GridBagConstraints.REMAINDER;
//            panel.add(tfRegisterObjName, gbc);
//            
//            tfRegisterClassName = new JTextField();
//            gbc.fill = GridBagConstraints.HORIZONTAL;
//            gbc.weightx = 2.0;
//            gbc.gridx = 1;
//            gbc.gridy = 0;
//            //gbc.gridwidth = GridBagConstraints.REMAINDER;
//            panel.add(tfRegisterClassName, gbc);
//            
//            tfUnregister = new JTextField();
//            gbc.fill = GridBagConstraints.HORIZONTAL;
//            gbc.weightx = 2.0;
//            gbc.gridx = 0;
//            gbc.gridy = 1;
//            //gbc.gridwidth = GridBagConstraints.REMAINDER;
//            panel.add(tfUnregister, gbc);
            
            ScrollPane spRegister = new ScrollPane();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1.0;
            gbc.gridx = 0;
            gbc.gridy = 0;
			panel.add(spRegister, gbc);
            
            ScrollPane spUnregister = new ScrollPane();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1.0;
            gbc.gridx = 1;
            gbc.gridy = 0;
            panel.add(spUnregister, gbc);
            
            lRegListModel= new DefaultListModel();
            
            lRegister = new JList(lRegListModel) {
                //Subclass JList to workaround bug 4832765, which can cause the
                //scroll pane to not let the user easily scroll up to the beginning
                //of the list.  An alternative would be to set the unitIncrement
                //of the JScrollBar to a fixed value. You wouldn't get the nice
                //aligned scrolling, but it should work.
                public int getScrollableUnitIncrement(Rectangle visibleRect,
                                                      int orientation,
                                                      int direction) {
                    int row;
                    if (orientation == SwingConstants.VERTICAL &&
                          direction < 0 && (row = getFirstVisibleIndex()) != -1) {
                        Rectangle r = getCellBounds(row, row);
                        if ((r.y == visibleRect.y) && (row != 0))  {
                            Point loc = r.getLocation();
                            loc.y--;
                            int prevIndex = locationToIndex(loc);
                            Rectangle prevR = getCellBounds(prevIndex, prevIndex);

                            if (prevR == null || prevR.y >= r.y) {
                                return 0;
                            }
                            return prevR.height;
                        }
                    }
                    return super.getScrollableUnitIncrement(
                                    visibleRect, orientation, direction);
                }
            };

            lRegister.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            lRegister.setLayoutOrientation(JList.HORIZONTAL_WRAP);
            lRegister.setVisibleRowCount(-1);
            spRegister.add(lRegister);
            
            lUnregListModel= new DefaultListModel();   
            
            
            lUnregister = new JList(lUnregListModel) {
                //Subclass JList to workaround bug 4832765, which can cause the
                //scroll pane to not let the user easily scroll up to the beginning
                //of the list.  An alternative would be to set the unitIncrement
                //of the JScrollBar to a fixed value. You wouldn't get the nice
                //aligned scrolling, but it should work.
                public int getScrollableUnitIncrement(Rectangle visibleRect,
                                                      int orientation,
                                                      int direction) {
                    int row;
                    if (orientation == SwingConstants.VERTICAL &&
                          direction < 0 && (row = getFirstVisibleIndex()) != -1) {
                        Rectangle r = getCellBounds(row, row);
                        if ((r.y == visibleRect.y) && (row != 0))  {
                            Point loc = r.getLocation();
                            loc.y--;
                            int prevIndex = locationToIndex(loc);
                            Rectangle prevR = getCellBounds(prevIndex, prevIndex);

                            if (prevR == null || prevR.y >= r.y) {
                                return 0;
                            }
                            return prevR.height;
                        }
                    }
                    return super.getScrollableUnitIncrement(
                                    visibleRect, orientation, direction);
                }
            };

            lUnregister.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            lUnregister.setLayoutOrientation(JList.HORIZONTAL_WRAP);
            lUnregister.setVisibleRowCount(-1);
            spUnregister.add(lUnregister);
            
            btRegister = new JButton();
            btRegister.setText("register");
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1.0;
            gbc.gridx = 0;
            gbc.gridy = 1;
            //gbc.gridwidth = GridBagConstraints.REMAINDER;
            panel.add(btRegister, gbc);           
            
            btUnregister = new JButton();
            btUnregister.setText("unregister");
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1.0;
            gbc.gridx = 1;
            gbc.gridy = 1;
            //gbc.gridwidth = GridBagConstraints.REMAINDER;
            panel.add(btUnregister, gbc);
            
            taLog = new JTextArea();
            taLog.setLineWrap(true);
            JScrollPane sbrTextArea = new JScrollPane(taLog);
            sbrTextArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            gbc.fill = GridBagConstraints.BOTH;
            gbc.weighty = 1.0;
            gbc.gridx = 0;
            gbc.gridy = 2;
            gbc.gridheight = GridBagConstraints.REMAINDER;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            panel.add(sbrTextArea, gbc);
            //taLog.setEditable(false);
            
            setVisible(true);
        }
        
        public void updateTaLog(String logMsg)
        {
                taLog.append(logMsg + newline);
        }
        
        public void clearLog()
        {
                taLog.setText("");
        }

}